package com.applet.utils.baidu;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class BaiduYingYanUtils {

    private static String ak = "IK5AlGXoZ23tDAGjldRalicbhdpsrKwE";

    private static BaiduYingYanUtil baiduYingYanUtil = new BaiduYingYanUtil();

    public static String geocoder(String lat,String lng){
        String location = lat + "," + lng;
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("callback","renderReverse");
        params.put("ak", ak);
        params.put("location", location);
        params.put("output", "json");
        params.put("pois",1);
        try {
            String json = baiduYingYanUtil.geocoder(params);
            String str=json.substring(29,json.length()-1);
            JSONObject jsonObject=new JSONObject(str);
            JSONObject result = jsonObject.getJSONObject("result");
            JSONObject addressComponent = result.getJSONObject("addressComponent");
            String city = addressComponent.getString("city");
            return city;
        }catch (JSONException e) {
            e.printStackTrace();
            return null;
        }

    }

    public static void main(String[] args){
        String city = geocoder("31.266302","121.557401");
        System.out.println(city);
    }


}
